require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroWeaponHandler = {};
local this = RedDotsHeroWeaponHandler;

local weaponConfig = nil;
local debrisConfig = nil;

function RedDotsHeroWeaponHandler.init()
    CloseEvent(Config.EventType.Update_Weapon, this.update);
    CloseEvent(Config.EventType.Update_WeaponAttrId, this.update);
    CloseEvent(Config.EventType.Reset_Weapon, this.update);
    CloseEvent(Config.EventType.DebrisUpdate, this.update);
    CloseEvent(Config.EventType.DebrisReload, this.update);
    CloseEvent(Config.EventType.DebrisMerage, this.update);
    CloseEvent(Config.EventType.Fresh_Bag, this.update);

    ListenEvent(Config.EventType.Update_Weapon, this.update);
    ListenEvent(Config.EventType.Update_WeaponAttrId, this.update);
    ListenEvent(Config.EventType.Reset_Weapon, this.update);
    ListenEvent(Config.EventType.DebrisUpdate, this.update);
    ListenEvent(Config.EventType.DebrisReload, this.update);
    ListenEvent(Config.EventType.DebrisMerage, this.update);
    ListenEvent(Config.EventType.Fresh_Bag, this.update);

    if weaponConfig == nil then
        weaponConfig = G.dataTable["t_weapon"];
    end
    if debrisConfig == nil then
        debrisConfig = G.dataTable["t_debris"];
    end

    return this;
end

--参数1 英雄id nil代表遍历所有英雄
function RedDotsHeroWeaponHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    local partnerId = ...;
    if partnerId ~= nil then
        local weapon = logicMgr.EquipManager.tabWeapon[partnerId];
        if weapon ~= nil then
            flag = this.getFlagByWeaponInfo(weapon, partnerId);
        end
    else
        for key, value in pairs(EquipManager.tabWeapon) do
            flag = this.getFlagByWeaponInfo(value,value.partnerId);
            if flag == Config.RedDotsFlagState.Needful then
                break ;
            end
        end
    end
    return flag;
end

function RedDotsHeroWeaponHandler.getFlagByWeaponInfo(weaponInfo, partnerId)
    if not FuncOpenManager.isOpen(Config.FuncOpenType.HeroWeapon, false, nil, { heroId = partnerId }) then
        return Config.RedDotsFlagState.None;
    end
    local isCan = true;
    local curHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(partnerId), true)
    local configId = 150000 + tonumber(curHeroData.partnerModelId);
    local config = G.dataTable["t_weapon"][tostring(configId)];
    if weaponInfo.level >= config.f_MaxLevel then
        isCan = false;
    else
        local weaponLvCostConfig = decodeJsonStrToTabel(config.f_WeaponEnergyCost);
        local lvCost = weaponLvCostConfig[weaponInfo.level + 1];
        local itemNum = logicMgr.ItemManager.getCurrencyCountByModelId(lvCost[1]);
        if itemNum < lvCost[2] then
            isCan = false;
        end
    end

    local needStars = decodeJsonStrToTabel(config.f_WeaponNeedStar);
    if weaponInfo.level > #needStars or needStars[weaponInfo.level + 1] > curHeroData.star then
        isCan = false;
    end
    if isCan then
        return Config.RedDotsFlagState.Needful;
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsHeroWeaponHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroWeapon);
end